package com.project.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.HashMap;

public class CreateToken {
      private static final long EXPIRE_TIME = 1000 * 60 * 60;
      public static final String TOKEN_SECRET = "13cd761e-8805-4294-8641-fc28fc3120a7";
      
      public static String createToken(String json) {
            
            //过期时间
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            //私钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //设置头信息
            HashMap<String, Object> header = new HashMap<>(2);
            header.put("typ", "JWT");
            header.put("alg", "HS256");
            //附带username和userID生成签名
            String token = JWT.create().withHeader(header)
                                   .withClaim("info", json)
                                   .withExpiresAt(date).sign(algorithm);
            return token;
      }
}
